<template>
  <!-- 导航栏 -->
  <van-nav-bar title="我的收货地址" left-text="返回" left-arrow @click-left="back" />

  <!-- 收货列表 -->

  <!-- 确认订单 -->
  <van-address-list
    v-if="action == 'order'"
    v-model="AddressDefault"
    :list="AddressList"
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
    @select="onSelect"
  />

  <!-- 从会员中心 -->
  <van-address-list
    v-else
    v-model="AddressDefault"
    :list="AddressList"
    default-tag-text="默认"
    @add="onAdd"
    @edit="onEdit"
    @select="onCheck"
  />

</template>
<script>
    export default {
        name: 'AddressIndex',
        emits: ['toggle'],
        created() {
            //触发父组件的事件
            this.$emit('toggle', false)

            this.action = this.$route.query.action ? this.$route.query.action: ''

            

            //一进入就去请求收货地址列表
            this.AddressData()
        },
        data(){
            return{
                LoginUser: this.$cookies.get('LoginUser'),
                //默认的收货地址
                AddressDefault: 0,
                //收货列表
                AddressList: [],
                //动作用于区分是从哪个界面过来的
                action: ''
            }
        },
        methods: {
            back() {
                //返回上一层界面
                this.$router.go(-1)
            },
            //获取收货地列表
            async AddressData(){

                var result = await this.$api.AddressIndex({userid: this.LoginUser.id})
           
                if(result.code == 1){

                    var data = result.data

                    var list = []

                     //先判断是否有收货地址
                    if(data.length <= 0)
                    {
                    //没有收货地址
                    this.$notify({
                        message:'暂无收货地址',
                        duration:1000,
                    })

                    return false
                    }

                    for(var item of data){

                        //找出默认收货地址
                        if(item.status == 1){
                            this.AddressDefault = item.id
                        }

                        //组装收货地址列表
                        var address = {
                            id: item.id,
                            name: item.consignee,
                            tel: item.mobile,
                            isDefault: item.status == 1 ? true : false
                        }

                        //详细地址
                        var info = ''

                        if(item.province)
                        {
                        info += `${item.provinces.name}`
                        }

                        if(item.city)
                        {
                        info += `${item.citys.name}`
                        }

                        if(item.district)
                        {
                        info += `${item.districts.name}`
                        }

                        if(item.address)
                        {
                        info += `${item.address}`
                        }

                        address.address = info

                        list.push(address)

                    }

                    this.AddressList = list
                }

            },
            
            //添加收货地址
            onAdd(){
                this.$router.push('/user/address/add')
            },
            //编辑收货地址
            onEdit(item){
                // console.log(item.id)
                this.$router.push({
                    path:'/user/address/edit',
                    query:{
                        id: item.id
                    }
                })
            },
            //切换地址
            async onCheck(item) {

                var data ={
                    addrid: item.id,
                    userid: this.LoginUser.id
                }

                var result = await this.$api.AddressCheck(data)

                if(result.code == 1){
                    this.AddressData()
                }
            },
            //从确认订单过来的，切换默认的收货地址
            onSelect(item){
                var data ={
                    id: item.id,
                    mobile: item.tel,
                    consignee: item.name
                }
                // 把地址信息存放到cookies
                this.$cookies.set('address', data)

                this.$router.go(-1)

            }
        }
    }
</script>